#include <REGX52.H>
#include "Delay.h"

#define ROW1 P1_7
#define ROW2 P1_6
#define ROW3 P1_5
#define ROW4 P1_4

#define COL1 P1_3
#define COL2 P1_2
#define COL3 P1_1
#define COL4 P1_0

char Matrix_num(void)//检测哪个按键被按下。如果没按则返回0；
{
	
		P1 = 0XFF;
		
		ROW1 = 0;
		
				if(COL1 == 0) { Delay(20); while(COL1 == 0); Delay(20); return 1;}
					
				if(COL2 == 0) { Delay(20); while(COL2 == 0); Delay(20); return 2;}
					
				if(COL3 == 0) { Delay(20); while(COL3 == 0); Delay(20); return 3;}
					
				if(COL4 == 0) { Delay(20); while(COL4 == 0); Delay(20); return 4;}
				
		P1 = 0XFF;
		
		ROW2 = 0;
		
				if(COL1 == 0) { Delay(20); while(COL1 == 0); Delay(20); return 5;}
					
				if(COL2 == 0) { Delay(20); while(COL2 == 0); Delay(20); return 6;}
					
				if(COL3 == 0) { Delay(20); while(COL3 == 0); Delay(20); return 7;}
					
				if(COL4 == 0) { Delay(20); while(COL4 == 0); Delay(20); return 8;}
		P1 = 0XFF;
		
		ROW3 = 0;
		
				if(COL1 == 0) { Delay(20); while(COL1 == 0); Delay(20); return 9;}
					
				if(COL2 == 0) { Delay(20); while(COL2 == 0); Delay(20); return 10;}
					
				if(COL3 == 0) { Delay(20); while(COL3 == 0); Delay(20); return 11;}
					
				if(COL4 == 0) { Delay(20); while(COL4 == 0); Delay(20); return 12;}
		P1 = 0XFF;
		
		ROW4 = 0;
		
				if(COL1 == 0) { Delay(20); while(COL1 == 0); Delay(20); return 13;}
					
				if(COL2 == 0) { Delay(20); while(COL2 == 0); Delay(20); return 14;}
				
				if(COL3 == 0) { Delay(20); while(COL3 == 0); Delay(20); return 15;}
					
				if(COL4 == 0) { Delay(20); while(COL4 == 0); Delay(20); return 16;}
		
	return 0;
	
}